package com.javabi.htmlbuilder.css.font;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.javabi.htmlbuilder.css.CSSBuilder;
import com.javabi.htmlbuilder.css.CSSDeclaration;
import com.javabi.htmlbuilder.css.CSSPropertyName;

/**
 * A Font Family
 */
public class FontFamily implements CSSDeclaration {

	/** The font name set. */
	private final Set<String> fontNameSet = new HashSet<String>();

	/**
	 * Creates a new font family.
	 * @param name the font name.
	 */
	public FontFamily(String name) {
		addFont(name);
	}

	/**
	 * Creates a new font family.
	 * @param names the font names.
	 */
	public FontFamily(String... names) {
		addFonts(names);
	}

	/**
	 * Creates a new font family.
	 * @param names the font names.
	 */
	public FontFamily(Collection<String> names) {
		addFonts(names);
	}

	/**
	 * Add the named font.
	 * @param name the font name.
	 */
	public void addFont(String name) {
		if (name.length() == 0) {
			throw new IllegalArgumentException("name is empty");
		}
		if (!fontNameSet.add(name)) {
			throw new IllegalArgumentException("duplicate font: '" + name + "'");
		}
	}

	/**
	 * Add the named fonts.
	 * @param names the font names.
	 */
	public void addFonts(String... names) {
		for (String name : names) {
			addFont(name);
		}
	}

	/**
	 * Add the named fonts.
	 * @param names the font names.
	 */
	public void addFonts(Collection<String> names) {
		for (String name : names) {
			addFont(name);
		}
	}

	/**
	 * Returns the property.
	 * @return the property.
	 */
	public CSSPropertyName getPropertyName() {
		return CSSPropertyName.FONT_FAMILY;
	}

	/**
	 * Append this to the given css builder.
	 * @param css the css builder.
	 * @return the css builder.
	 */
	public CSSBuilder appendTo(CSSBuilder css) {
		css.property(getPropertyName());
		css.names(fontNameSet);
		return css;
	}

}
